package javase2.day04;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Demo2 {
	public static void main(String[] args) throws IOException{
		String file = "/home/tarena/test/week03.jpg";
		IOUtils.print(file);
		InputStream in = new FileInputStream(file);
		//是从in中读取byte序列，反序列化为img对象
		BufferedImage img=ImageIO.read(in);
		in.close();
		
		JFrame frame = new JFrame("Image IO");
		JPanel panel = new JPanel();
		JLabel label = new JLabel(new ImageIcon(img));
		frame.add(panel);
		panel.add(label);
		frame.setVisible(true);
		frame.setSize(300,200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Graphics pen = img.getGraphics();
		pen.setColor(Color.black);
		pen.drawString("By by", 50, 30);
		
		String outFile = "/home/tarena/week.jpg";
		OutputStream out = new FileOutputStream(outFile);
		//将img对象按照“png”格式，序列化为byte序列，写入到out流中
		ImageIO.write(img, "png", out);
		out.close();
	}
}
